﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace GUI
{
    public partial class FormQLChucVu : Form
    {
        public FormQLChucVu()
        {
            InitializeComponent();
        }
        public FormNhanVien frmCha;
        //public delegate void GetString(String TenCVmoi);
        //public GetString CapNhat;

        private void bntThemCV_Click(object sender, EventArgs e)
        {
            frmEditCV frm = new frmEditCV();
            frm.frmCha = this;
            frm.CvDTO = new CHUCVUDTO();
            frm.CvDTO.MaCV = -1;
            frm.ShowDialog();
        }
        public void Refesh(object sender, EventArgs e)
        {
            FormQLChucVu_Load(sender, e);
        }

        private void loadChucVu()
        {
            CHUCVUBUS CvBUS = new CHUCVUBUS();
            DataTable dt = new DataTable();
            dt = CvBUS.LayDSCV();
            dtgvDSCV.DataSource = null;
            dtgvDSCV.DataSource = dt;
        }

        private void FormQLChucVu_Load(object sender, EventArgs e)
        {
            loadChucVu();
            dtgvDSCV.CurrentCell = dtgvDSCV[0, 0];
            dtgvDSCV.Rows[0].Selected = true;
        }

        private void bntSuaCV_Click(object sender, EventArgs e)
        {
            frmEditCV frm = new frmEditCV();
            frm.frmCha = this;
            frm.CvDTO = new CHUCVUDTO();
            frm.CvDTO.MaCV = int.Parse(dtgvDSCV.CurrentRow.Cells[0].Value.ToString());
            frm.CvDTO.TenCV = dtgvDSCV.CurrentRow.Cells[1].Value.ToString();
            frm.ShowDialog();
        }

        private void bntThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        CHUCVUDTO CvDTO;
        private void dtgvDSCV_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            CvDTO = new CHUCVUDTO(); 
            CvDTO.MaCV = int.Parse(dtgvDSCV.CurrentRow.Cells[0].Value.ToString());
            CvDTO.TenCV = dtgvDSCV.CurrentRow.Cells[1].Value.ToString();
            //MessageBox.Show("MA CV la: " + CvDTO.MaCV + "\n TenCV la: " + CvDTO.TenCV);
        }

        private void bntXoaCV_Click(object sender, EventArgs e)
        {
            string cv = dtgvDSCV.CurrentRow.Cells[1].Value.ToString();
            if (MessageBox.Show("Bạn có muốn xóa chức vụ " + cv +
                " không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                == DialogResult.No) return;
            CHUCVUBUS CvBUS = new CHUCVUBUS();
            CHUCVUDTO CvDTO = new CHUCVUDTO();
            CvDTO.MaCV = int.Parse(dtgvDSCV.CurrentRow.Cells[0].Value.ToString());
            int ktChucVu = CvBUS.ktCV(CvDTO);
            if (ktChucVu > 0)
            {
                MessageBox.Show("Chức vụ này vẫn còn Nhân Viên nắm giữ, không thể xóa!",
                    "Thông báo",MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }
            CvBUS.XoaCV(CvDTO);
            MessageBox.Show("Xóa thành công!",
                   "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.None);
            FormQLChucVu_Load(sender, e);
        }

        private void FormQLChucVu_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmCha.FormNhanVien_Load(sender, e);
        }
    }
}
